package com.financing.platform.web.api;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.financing.platform.util.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.financing.platform.common.Pager;
import com.financing.platform.common.ResultCode;
import com.financing.platform.common.ResultMap;
import com.financing.platform.common.ResultMap.STATUS;
import com.financing.platform.dto.AccountWithdrawDTO;
import com.financing.platform.dto.BankCardDTO;
import com.financing.platform.dto.FaUserDTO;
import com.financing.platform.dto.FpPaymentDTO;
import com.financing.platform.dto.UserInfoDTO;
import com.financing.platform.dto.WallettWithdrawDTO;
import com.financing.platform.dto.YouDanAmountDTO;
import com.financing.platform.service.IAccountWithdrawService;
import com.financing.platform.service.IBankCardService;
import com.financing.platform.service.IFaUserService;
import com.financing.platform.service.IPaymentService;
import com.financing.platform.service.IUserInfoService;
import com.financing.platform.service.IYouDanAmountService;
import com.financing.platform.util.DTOdateSet;
import com.financing.platform.vo.AccountWithdrawVO;
import com.financing.platform.vo.BankCardVO;
import com.financing.platform.vo.WallettWithdrawVO;

/**
 * 
 * @ClassName: AccountWithdrawAction
 * @Description: 提现Action 
 * @author  lisu
 *
 */
@Controller("accountWithdrawActionAPI")
@RequestMapping("/api")
@Scope("prototype")
public class AccountWithdrawAction extends BaseAction{
	
	@Autowired
	private IAccountWithdrawService accountWithdrawServiceImpl;
	@Autowired
	private IYouDanAmountService youDanAmountServiceImpl;
	@Autowired
	private IPaymentService paymentSericeImpl;
	@Autowired
	private IBankCardService bankCardServiceImpl;
	/**
	 * 
	 * @Title: queryAccountWithdraw
	 * @Description: 提现 
	 * @param @param request
	 * @param @param response
	 * @param @param AccountWithdrawVO
	 * @param @return   
	 * @return ModelAndView   
	 * @throws
	 */
	@SuppressWarnings("unchecked")
	@RequestMapping("/withdraw/queryAccountWithdraw.shtml")
	@ResponseBody
	public ResultMap queryBankCard(HttpServletRequest request,
			HttpServletResponse response,AccountWithdrawVO accountWithdrawVo,Pager pager) {
		
		this.log.info("begin queryAccountWithdraw info!");
		
		try{
			
			accountWithdrawVo.setBegin(pager.getStartIndex());
			accountWithdrawVo.setPageSize(pager.getPageSize());
			
			int count = accountWithdrawServiceImpl.queryAccountWithdrawListCount(accountWithdrawVo);
			pager.setTotalCount(count);
			
			List<AccountWithdrawVO> list;
			if(count>0){
				list =  accountWithdrawServiceImpl.queryAccountWithdrawList(accountWithdrawVo);
			}else{
				list = Collections.EMPTY_LIST;
			}
			
			resultMap.setAttribute("list", list);
			resultMap.setAttribute("pager", pager);
			resultMap.setStatus(STATUS.SUCCESS);
			resultMap.setStatusCode(ResultCode.RESULT_CODE_SUCCESS);
			
		}catch(Exception e){
			this.log.error("error queryAccountWithdraw",e);
			resultMap.setStatus(STATUS.FAILE);
			resultMap.setStatusCode(ResultCode.RESULT_CODE_SYSTEM_ERROR);
		}
		this.log.info("end queryAccountWithdraw info access !");
		return resultMap;
	}
	/**
	 * 
	 * @Title: queryAccountWithdrawDetail
	 * @Description: 提现 明细
	 * @param @param request
	 * @param @param response
	 * @param @param AccountWithdrawVO
	 * @param @return   
	 * @return ModelAndView   
	 * @throws
	 */
	@RequestMapping("/withdraw/queryAccountWithdrawDetail.shtml")
	@ResponseBody
	public ResultMap queryAccountWithdrawDetail(HttpServletRequest request,
			HttpServletResponse response,Integer id) {
		
			this.log.info("In the queryAccountWithdrawDetail begin!");
		
		try{
			
			AccountWithdrawVO accountWithdrawVO = accountWithdrawServiceImpl.queryAccountWithdrawInfo(id);
			
			resultMap.setAttribute("dto", accountWithdrawVO);
			resultMap.setStatus(STATUS.SUCCESS);
			resultMap.setStatusCode(ResultCode.RESULT_CODE_SUCCESS);
			
			this.log.info("In the queryAccountWithdrawDetail end!");
			
		}catch(Exception e){
			this.log.error("error queryAccountWithdraw",e);
			resultMap.setStatus(STATUS.FAILE);
			resultMap.setStatusCode(ResultCode.RESULT_CODE_SYSTEM_ERROR);
		}
		return resultMap;
	}
	/**
	 * 
	 * @Title: updateWithdrawStatus
	 * @Description: 提现-修改状态
	 * @param @param request
	 * @param @param response
	 * @param @param accountWithdrawDTO
	 * @param @return   
	 * @return ModelAndView   
	 * @throws
	 */
	@RequestMapping("/withdraw/updateWithdrawStatus.shtml")
	@ResponseBody
	public ResultMap updateWithdrawStatus(HttpServletRequest request,
			HttpServletResponse response,AccountWithdrawDTO accountWithdrawDTO) {
		
			this.log.info("In the  updateWithdrawStatus begin!");
		
		try{
			//赋值
			DTOdateSet.setUpdateInfo(accountWithdrawDTO, request.getSession());
			//提现-修改状态
			accountWithdrawServiceImpl.updateAccountWithdraw(accountWithdrawDTO);
			
			resultMap.setStatus(STATUS.SUCCESS);
			resultMap.setStatusCode(ResultCode.RESULT_CODE_SUCCESS);
			
			this.log.info("In the  updateWithdrawStatus end!");
		}catch(Exception e){
			this.log.error("error queryAccountWithdraw",e);
			resultMap.setStatus(STATUS.FAILE);
			resultMap.setStatusCode(ResultCode.RESULT_CODE_SYSTEM_ERROR);
		}
	
		return resultMap;
	}
	// ****************************有单模式 钱包相关***********************************
		/**
		 * 提现列表
		 * @param request
		 * @param response
		 * @param accountWithdrawVo
		 * @param pager
		 * @return
		 */
		@RequestMapping("/withdraw/wallettWithdrawList.shtml")
		@ResponseBody
		public ResultMap queryWallettWithdrawList(HttpServletRequest request,
				HttpServletResponse response,WallettWithdrawVO wallettWithdrawVO) {
			ResultMap result = new ResultMap();
			Pager pager = new Pager();
			String pageNo = request.getParameter("pageNo");
			String pageSize = request.getParameter("pageSize");
			if(StringUtil.isNullOrEmptyStr(pageNo)){
				pageNo="1";
			}
			if(StringUtil.isNullOrEmptyStr(pageSize)){
				wallettWithdrawVO.setPageSize(10);
			}
			try {

				Integer count = youDanAmountServiceImpl.getYouDanWithdrawListCount(wallettWithdrawVO);
				pager.setTotalCount(count);
				pager.setPageSize(wallettWithdrawVO.getPageSize());
				pager.setPageNo(Integer.parseInt(pageNo));
				wallettWithdrawVO.setBegin(pager.getStartIndex());
				List<WallettWithdrawDTO> wallettWithdrawList = youDanAmountServiceImpl.getYouDanWithdrawList(wallettWithdrawVO);
				
				result.setAttribute("pager", pager);
				result.setAttribute("result", wallettWithdrawList);
				result.setStatus(STATUS.SUCCESS);
				result.setStatusCode(ResultCode.RESULT_CODE_SUCCESS);
			} catch (Exception e) {
				this.log.error("wallet withdrawList error:",e);
				result.setStatus(STATUS.ERROR);
				result.setStatusCode(ResultCode.RESULT_CODE_SYSTEM_ERROR);
			}
			return  result;
		}
		@RequestMapping("/withdraw/wallettWithdrawDetail.shtml")
		@ResponseBody
		public ResultMap queryWallettWithdrawDetail(HttpServletRequest request,
				HttpServletResponse response, WallettWithdrawVO wallettWithdrawVO) {
			ResultMap result = new ResultMap();
			List<WallettWithdrawDTO> resultList = new ArrayList<WallettWithdrawDTO>();
			try {
				
				WallettWithdrawDTO wallettWithdrawDTO = youDanAmountServiceImpl.getYouDanWithdrawList(wallettWithdrawVO).get(0);
				// 查询卡号等信息

				// 查询银行卡info
				BankCardDTO bankCard = new BankCardDTO();
				bankCard = youDanAmountServiceImpl.queryFaUserBankInfo(wallettWithdrawDTO);
				wallettWithdrawDTO.setCardNo(bankCard.getCardNumber());// 卡号
				wallettWithdrawDTO.setCardCarrier(bankCard.getUserName());// 姓名
				resultList.add(wallettWithdrawDTO);
				// 查询对应支付情况
				List<FpPaymentDTO> fpPaymentDTOList = paymentSericeImpl.loadByAmountDetail(wallettWithdrawDTO.getAssociationId(),wallettWithdrawDTO.getAmountType());
				result.setAttribute("payList", fpPaymentDTOList);
				result.setAttribute("result", resultList);
				result.setStatus(STATUS.SUCCESS);
				result.setStatusCode(ResultCode.RESULT_CODE_SUCCESS);
			} catch (Exception e) {
				this.log.error("wallet withdrawList error:",e);
				result.setStatus(STATUS.ERROR);
				result.setStatusCode(ResultCode.RESULT_CODE_SYSTEM_ERROR);
			}
			return  result;
		}
		/**
		 * 提现成功
		 * @param request
		 * @param response
		 * @param accountWithdrawVo
		 * @param pager
		 * @return
		 */
		@RequestMapping("/withdraw/wallettWithdrawSuccess.shtml")
		@ResponseBody
		public ResultMap wallettWithdrawSuccess(HttpServletRequest request,
				HttpServletResponse response,WallettWithdrawVO wallettWithdrawVO) {
			ResultMap result = new ResultMap();
			int i = -1;
			YouDanAmountDTO youDanAmount = new YouDanAmountDTO();
			try {
				youDanAmount.setId(wallettWithdrawVO.getId());
				youDanAmount = youDanAmountServiceImpl.getYouDanAmount(youDanAmount);
				if(null != youDanAmount && youDanAmount.getOperateType().endsWith("03")){// 03  提现中
					if(youDanAmount.getAmountType().equals("01")){// 保证金
						i = youDanAmountServiceImpl.updateBaoZhengJin2WithdrawSuccess(youDanAmount.getAssociationId(), wallettWithdrawVO.getRemark(),wallettWithdrawVO.getPlatSerialNo()) ;
						
					}else{// 佣金
						i = youDanAmountServiceImpl.updateYongJin2WithdrawSuccess(youDanAmount, wallettWithdrawVO.getRemark(),wallettWithdrawVO.getPlatSerialNo()) ;
					}
				}
				result.setAttribute("result",i );
				result.setStatus(STATUS.SUCCESS);
				result.setStatusCode(ResultCode.RESULT_CODE_SUCCESS);
			} catch (Exception e) {
				this.log.error("wallet wallettWithdrawSuccess error:",e);
				result.setStatus(STATUS.ERROR);
				result.setStatusCode(ResultCode.RESULT_CODE_SYSTEM_ERROR);
			}
			return  result;
		}
		/**
		 * 提现失败
		 * @param request
		 * @param response
		 * @param accountWithdrawVo
		 * @param pager
		 * @return
		 */
		@RequestMapping("/withdraw/wallettWithdrawFail.shtml")
		@ResponseBody
		public ResultMap wallettWithdrawFail(HttpServletRequest request,
				HttpServletResponse response,WallettWithdrawVO wallettWithdrawVO) {
			ResultMap result = new ResultMap();
			YouDanAmountDTO youDanAmount = new YouDanAmountDTO();
			int i = -1;
			try {
				youDanAmount.setId(wallettWithdrawVO.getId());
				youDanAmount = youDanAmountServiceImpl.getYouDanAmount(youDanAmount);
				if(null != youDanAmount && youDanAmount.getOperateType().endsWith("03")){// 03  提现中
					if(youDanAmount.getAmountType().equals("01")){// 保证金
						i = youDanAmountServiceImpl.updateBaoZhengJin2WithdrawFail(youDanAmount.getAssociationId(), wallettWithdrawVO.getRemark(),wallettWithdrawVO.getPlatSerialNo()) ;
						
					}else if(youDanAmount.getAmountType().equals("02")){// 佣金
						i = youDanAmountServiceImpl.updateYongJin2WithdrawFail(youDanAmount.getAssociationId(), wallettWithdrawVO.getRemark(),wallettWithdrawVO.getPlatSerialNo()) ;
					}
				}
				result.setAttribute("result", i);
				result.setStatus(STATUS.SUCCESS);
				result.setStatusCode(ResultCode.RESULT_CODE_SUCCESS);
			} catch (Exception e) {
				this.log.error("wallet wallettWithdrawFail error:",e);
				result.setStatus(STATUS.ERROR);
				result.setStatusCode(ResultCode.RESULT_CODE_SYSTEM_ERROR);
			}
			return  result;
		}
}
